package com.gemframework.controller;

import com.gemframework.common.annotation.ApiAuth;
import com.gemframework.common.util.ApiLogUtils;
import com.gemframework.common.util.ApiResult;
import com.gemframework.common.util.BaseApiController;
import com.gemframework.common.util.JwtUtil;
import com.gemframework.common.utils.AsyncSchedulerUtil;
import com.gemframework.common.utils.GemRedisUtils;
import com.gemframework.mapper.CustomTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: ApiTestController
 * Description:
 * date: 2020/4/13 22:45
 *
 * @author afeng
 * @since JDK 1.8
 */
@Validated
@RestController
@RequestMapping("/test")
public class ApiTestController extends BaseApiController {

    @Autowired
    private GemRedisUtils<Object> redisUtils;


    @Autowired
    private CustomTableMapper customTableDao;

    @GetMapping("/redis")
    public ApiResult other() {
        redisUtils.set("test", "aaaaaaaaaaaaaa", 60 * 5);
        return success(redisUtils.get("test"));
    }

    @GetMapping("/token")
    public ApiResult token() {
        String uid = getParameter("uid");
        String refreshToken = JwtUtil.createToken(uid);
        return success(refreshToken);
    }

    @GetMapping("/authToken")
    public ApiResult authToken() {
        return success(JwtUtil.getUserId());
    }

    @GetMapping("/bd")
    public ApiResult bd(String uid) {
        Map<String, Object> where = new HashMap<>();
        where.put("id", uid);
        Map<String, Object> user = customTableDao.selectOne("app_user", where);
        return success(user);
    }

    @ApiAuth(isAuthSign = true)
    @GetMapping("/authSign")
    public ApiResult authSign() {
        return success();
    }

    @GetMapping("testData")
    public ApiResult testData(@NotEmpty(message = "用户ID不能为空") String uid, @NotEmpty(message = "test不能为空") String test) {
        return success(uid);
    }

    @GetMapping("testAsync")
    public ApiResult testAsync() {

        AsyncSchedulerUtil.execute(new Runnable() {
            @Override
            public void run() {
                ApiLogUtils.debugPrint("打印");
                System.out.println("aaaaa");
            }
        });

        return success();
    }

}
